#pragma once
#include "stdafx.h"
#include "one.h"
#include "cvaux.h"
#include "cxmisc.h"
#include "highgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

//VARIABLES for CODEBOOK METHOD:
CvBGCodeBookModel* model = 0;
const int NCHANNELS = 3;
bool ch[NCHANNELS]={true,true,true}; // This sets what channels should be adjusted for background bounds

int nframes=0;
   IplImage* yuvImage = 0; 
    IplImage* ImaskCodeBook = 0;
	IplImage *ImaskCodeBookCC=0;


void reset_()
{
	       cvBGCodeBookClearStale( model, 0 );
           nframes = 0;

}

void set(int min_,int max_,int bounds_)
{
 //Set color thresholds to default values
    model->modMin[0] = min_;
    model->modMin[1] = model->modMin[2] = min_;
    model->modMax[0] = max_;
    model->modMax[1] = model->modMax[2] = max_;
    model->cbBounds[0] = model->cbBounds[1] = model->cbBounds[2] = bounds_;

}
void initialise()
{
	 
    model = cvCreateBGCodeBookModel();
    
    //Set color thresholds to default values
    model->modMin[0] = 20;
    model->modMin[1] = model->modMin[2] = 20;
    model->modMax[0] = 35;
    model->modMax[1] = model->modMax[2] = 35;
    model->cbBounds[0] = model->cbBounds[1] = model->cbBounds[2] = 35;

}

void code(IplImage* rawImage,int *load_)
{
   
 
   

    
    int nframesToLearnBG = 300;



 

   

    //MAIN PROCESSING LOOP:

      
           
            ++nframes;
          *load_=nframes;
  
       
        
        //First time:
        if( nframes == 1  )
        {
            // CODEBOOK METHOD ALLOCATION
            yuvImage = cvCloneImage(rawImage);
            ImaskCodeBook = cvCreateImage( cvGetSize(rawImage), IPL_DEPTH_8U, 1 );
			ImaskCodeBookCC = cvCreateImage( cvGetSize(rawImage), IPL_DEPTH_8U, 1 );
            
            cvSet(ImaskCodeBook,cvScalar(255));
            
            
        }

        // If we've got an rawImage and are good to go:                
       
            cvCvtColor( rawImage, yuvImage, CV_BGR2YCrCb );//YUV For codebook method
            //This is where we build our background model
            if( nframes-1 < nframesToLearnBG  )
                cvBGCodeBookUpdate( model, yuvImage );

            if( nframes-1 == nframesToLearnBG  )
                cvBGCodeBookClearStale( model, model->t/2 );
            
            //Find the foreground if any
            if( nframes-1 >= nframesToLearnBG  )
            {
                // Find foreground by codebook method
                cvBGCodeBookDiff( model, yuvImage, ImaskCodeBook );
                // This part just to visualize bounding boxes and centers if desired
                cvCopy(ImaskCodeBook,ImaskCodeBookCC);	
                cvSegmentFGMask( ImaskCodeBookCC );
				nframes=302;
            }
           
           
        cvZero( rawImage );
        cvCvtPlaneToPix( ImaskCodeBookCC,ImaskCodeBookCC, ImaskCodeBookCC, 0, rawImage );
       
       


}
